package com.matt.docpro.config;

import cn.hutool.core.date.DateUtil;
import com.matt.docpro.model.User;
import com.matt.docpro.service.LoginInfoService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Utils
 * Created by on 2024/02/29.
 */
public class Utils {
    @Autowired
    LoginInfoService loginInfoService;

    static String uploadPath = System.getProperty("user.dir").replace("\\","/")+"/user-icon/";

    public static String uploadFile(MultipartFile iconFile, String id) {
        if (iconFile != null && !iconFile.getOriginalFilename().equals("")) {
            try {
                String imgFormat = iconFile.getOriginalFilename().substring(iconFile.getOriginalFilename().lastIndexOf("."));
                // 指定保存路径
                String fileName = id;
                String filePath = uploadPath + "\\" + fileName + imgFormat;
                File file = new File(uploadPath);
                //如果文件夹不存在则会创建
                if (!file.exists()){
                    file.mkdirs();
                }
                // 保存文件到本地
                iconFile.transferTo(new File(filePath));
                return "/upload/"+fileName + imgFormat;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        } else {
            return null;
        }
    }

    public static String getToken(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")){
                    return cookie.getValue();
                }

            }
        }
        return null;
    }

    public static String getWeekDay(Date date) {
        int weekNum = DateUtil.dayOfWeek(date);
        String res = "星期";
        switch (weekNum) {
            case 1:
                res += "日";
                break;
            case 2:
                res += "一";
                break;
            case 3:
                res += "二";
                break;
            case 4:
                res += "三";
                break;
            case 5:
                res += "四";
                break;
            case 6:
                res += "五";
                break;
            case 7:
                res += "六";
                break;
        }
        return res;
    }
}
